//
//  MLBLEManager.h
//  喵喵屋
//
//  Created by 程杰 on 2020/11/11.
//  Copyright © 2020 cwq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

NS_ASSUME_NONNULL_BEGIN

@class MLBleParamsItem;
@class MLBleResultInfos;

@interface MLBLEModel : NSObject
///开启配网标记，0X00（已配网）/0X01(cobo需要配网)/0x02（ble模块需要配网）直接配网
@property (nonatomic,assign) NSInteger mlApState;

/**
    0 -蓝牙名称是"MLink_"开头-6类型;
    1-广播协议6667开头-7类型;
    2-火星人FA蓝牙
 */
@property (nonatomic,assign) NSInteger bleType;

/// 信号强度
@property (nonatomic,strong) NSNumber *RSSI;
/// kind
@property (nonatomic,copy) NSString *mlKind;
/// model
@property (nonatomic,copy) NSString *mlModel;
/// 协议版本号
@property (nonatomic,copy) NSString *mlVersionCode;
/// 是否是妙联的蓝牙
@property (nonatomic,assign) BOOL isMLble;
/// 设备图片
@property (nonatomic,copy) NSString *netImg;
/// 设备名字
@property (nonatomic,copy) NSString *netName;
/// 搜索到的蓝牙名字
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *mac;
// 如阿里云：CodeStringValue
@property (nonatomic,copy) NSString *deviceName;
@property (nonatomic,strong) CBPeripheral *peripheral;
@property (nonatomic,strong) CBService *service;
//是否连接
@property (nonatomic,assign) BOOL connect;

///  广播数据
@property (nonatomic,copy) NSString *manufacture;
//接收广播数据
@property (nonatomic,strong) NSDictionary *advertisementData;

@end


@class WQMLBleDeviceInfo;

typedef NS_ENUM(NSUInteger, MLBLEConnectState) { // 保留
    MLBLEConnectStateOvertime , // 超时
    MLBLEConnectStateBegin , // 进入连接状态
    MLBLEConnectStateSetupNetOk , // 已经配网过
    MLBLEConnectStateNone , // 未配网
    MLBLEConnectStateConnecting, // 配网中
    MLBLEConnectStateApSuccess, //
    MLBLEConnectStateSuccess, // 连接成功
    MLBLEConnectStateError, // 连接失败
    MLBLEConnectDeviceSuccess, // 和设备蓝牙成功
    MLBLEConnectDiscoverServiceSuccess, // 发现外设成功
    MLBLEConnectDeviceNotifyOK, //
    MLBLEConnectStateDisconnect, // 外设断开连接
    MLBLEConnectStateConnectFail, // 连接外设失败
    MLBLEConnectStateUnKnow, // 模块返回状态未识别
};

typedef NS_ENUM(NSUInteger, MLBLEConnectCBState) {
    MLBLEConnectCBDisconnect,//  外设断开连接
    MLBLEConnectCBSuccess, // 连接外设成功
    MLBLEConnectCBNotifyOK,
};

/// 妙联返回状态信息
typedef void (^mlBleResultInfo)(MLBleResultInfos *result);

/// 返回蓝牙连接状态信息
typedef void (^mlBleCBState )(MLBLEConnectCBState state);

/// 蓝牙信息model
typedef void (^mlBleDiscoverPeripheral )(MLBLEModel *model);


/// 妙联蓝牙连接状态回调 - 保留
typedef void (^mlBleConnectStateBlock)(MLBleResultInfos *result);

@interface MLBLEManager : NSObject <CBCentralManagerDelegate,CBPeripheralDelegate>

/// 单例
+(instancetype)sharedManager;

/// 初始化开始
- (void)mlBLeRun;
/// 扫描蓝牙
- (void)mlBleScan;
/// 停止扫描
- (void)mlBleStopScan;

/// 可选方法
/// @param isGet 是否获取devicename
- (void)isGetDeviceName:(BOOL)isGet;

/// 返回蓝牙model
/// @param callBack model
- (void)mlBleSearchCallBack:(mlBleDiscoverPeripheral )callBack;

/// 返回从蓝牙广播中获取到信息
/// @param item 蓝牙广播数据
- (MLBLEModel *)getBleInfoWithBroadcast:(MLBLEModel *)item;

// 蓝牙的状态：连接、断开等
- (void)mlBleCBStateLinsten:(mlBleCBState)cbState;

// 配网
- (void)mlbleApConfig:(MLBleParamsItem *)params resultBlock:(mlBleResultInfo)callback;
// 串口数据，结果直接回到了串口；如果从串口发送手机端才能收到
- (void)sendUartData:(MLBleParamsItem *)params resultBlock:(mlBleResultInfo)callback;
// ota 升级
- (void)startOtaOnline:(MLBleParamsItem *)params resultBlock:(mlBleResultInfo)callback;
// 获取设备信息
- (void)getDeviceInfo:(MLBleParamsItem *)params resultBlock:(mlBleResultInfo)callback;

// 连接外设
- (void)mlBleConnectCBPeripheralModel:(MLBLEModel *)model;
/// 断开蓝牙
- (void)mlBleDisconnectPeripheral:(MLBLEModel *)model;

@end

@interface MLBleParamsItem : NSObject

@property (nonatomic,strong) MLBLEModel *bleModel;
/// wifi密码
@property (nonatomic,copy) NSString *wifiPassWorld;
/// wifi名称
@property(nonatomic, copy) NSString *wifiName;
/// 超时时间，默认60秒，可不填
@property(nonatomic, assign) int overTime;

@property (nonatomic,copy) NSString *sendType;

///只有是lot必填，其他选填，三方lot、例如亚马逊平台（PF和Port需要为NSNumber类型)
@property (nonatomic,strong) NSDictionary *lotDict;

@end

@interface MLBleResultInfos : NSObject
/// 保留字段
@property (nonatomic,copy) NSString *bleType;
/// 最后状态bleModel
@property (nonatomic,strong) MLBLEModel *bleModel;
/// 暂时不用；保留
@property (nonatomic,assign) MLBLEConnectState connectState;
/// lot -配网返回结果信息
@property(nonatomic, copy) NSString *message;
/// lot - code
@property(nonatomic, assign) int code;

/// 返回全部结果：
@property (nonatomic,strong) NSDictionary *resultDict;
@property (nonatomic, copy) NSString *resultStr;

@end

NS_ASSUME_NONNULL_END
